package com.dong.sharding.controller;

import com.dong.sharding.entity.LogEntity;
import com.dong.sharding.repo.LogRepo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Example;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Slf4j
@RequiredArgsConstructor
@RequestMapping("/log")
@RestController
public class TestController {

    private final LogRepo logRepo;

    @PostMapping("/save")
    public void save() {

        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, +1);
        Date time = calendar.getTime();

        LogEntity obj = new LogEntity();
        obj.setCreateBy("utyun");
        obj.setCreateTime(time);
        obj.setCode("code-3");
        logRepo.save(obj);
    }

    @GetMapping("/query")
    public void query(@RequestParam String code) {

        String dateStr = "2025-05-01 00:00:00";
        String dateEnd = "2025-05-31 23:59:59";

        // 定义格式，注意与字符串匹配
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        // 转换为 LocalDateTime
        LocalDateTime ldt = LocalDateTime.parse(dateStr, formatter);
        LocalDateTime edt = LocalDateTime.parse(dateEnd, formatter);

        // 如果需要转成 Date 类型
        Date start = Date.from(ldt.atZone(ZoneId.systemDefault()).toInstant());
        Date end = Date.from(edt.atZone(ZoneId.systemDefault()).toInstant());

        List<LogEntity> lst = logRepo.findByCreateTimeBetween(start, end);
        log.info("lst:{}", lst.size());
    }


}
